/*
 * DeliveryFragment.java
 * business
 *
 * Created by SunDing on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.agents.delivery;

import android.view.View;
import android.widget.ListView;

import com.yidiandao.business.R;
import com.yidiandao.business.base.SuperBaseFragment;
import com.yidiandao.business.manager.AgentsManager;
import com.yidiandao.business.ui.adapter.DeliveryOrderAdapter;
import com.yidiandao.business.ui.widget.dialog.LoadingDialog;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

import in.srain.cube.views.loadmore.LoadMoreContainer;
import in.srain.cube.views.loadmore.LoadMoreHandler;
import in.srain.cube.views.loadmore.LoadMoreListViewContainer;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * 我是代理商. 配送订单-View
 */
public class DeliveryFragment extends SuperBaseFragment implements DeliveryContract.View {

    private static final String TAG = DeliveryFragment.class.getSimpleName();

    private DeliveryContract.Presenter mPresenter;

    private ListView mListView;

    private PtrClassicFrameLayout mPtrFrame;

    private DeliveryOrderAdapter mDeliveryOrderAdapter;
    private LoadMoreListViewContainer mLoadMoreContainer;
    private LoadingDialog mDialog;

    public static DeliveryFragment getInstance() {
        return new DeliveryFragment();
    }

    @Override
    protected void initViews() {
        mDialog = new LoadingDialog(mActivity);

        mLoadMoreContainer =
                (LoadMoreListViewContainer) mRootView.findViewById(R.id.load_more_container);

        mPtrFrame = (PtrClassicFrameLayout) mRootView.findViewById(R.id.ptr_frame);
        mListView = (ListView) mRootView.findViewById(R.id.list_view);
        mDeliveryOrderAdapter = new DeliveryOrderAdapter(mActivity);
        mListView.setAdapter(mDeliveryOrderAdapter);

        initEvent();
        mPtrFrame.setLastUpdateTimeRelateObject(this);
    }

    @Override
    protected void startLoadData() {
        mPresenter.start();
    }

    private void initEvent() {

        // 下拉刷新
        mPtrFrame.setPtrHandler(
                new PtrDefaultHandler() {
                    @Override
                    public boolean checkCanDoRefresh(
                            PtrFrameLayout frame, View content, View header) {
                        return PtrDefaultHandler.checkContentCanBePulledDown(
                                frame, mListView, header);
                    }

                    @Override
                    public void onRefreshBegin(PtrFrameLayout frame) {
                        mPresenter.start();
                    }
                });
        // 上拉加载更多
        mLoadMoreContainer.useDefaultFooter();
        mLoadMoreContainer.setLoadMoreHandler(
                new LoadMoreHandler() {
                    @Override
                    public void onLoadMore(LoadMoreContainer loadMoreContainer) {
                        mPresenter.getNetData(false);
                    }
                });

        AgentsManager.getInstance()
                .setOnButtonClickListener(
                        new AgentsManager.onButtonClickListener() {
                            @Override
                            public void onClick(int type, String orderId) {
                                mPresenter.handleEvent(type, orderId);
                            }
                        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_delivery;
    }

    @Override
    public void setPresenter(DeliveryContract.Presenter presenter) {

        mPresenter = presenter;
    }

    @Override
    public String getPagerTag() {
        return TAG;
    }

    @Override
    public void showToast(String text) {
    }

    @Override
    public void setDisplayListItems(List<DisplayListItem> displayListItems) {
        mDeliveryOrderAdapter.setData(displayListItems);
    }

    @Override
    public void refreshContent(int result) {
        superRefreshLayoutState(result);
    }

    @Override
    public int checkResultObject(Object resultObject) {
        return superCheckListState(resultObject);
    }

    @Override
    public void setRefreshComplete() {
        mPtrFrame.refreshComplete();
    }

    @Override
    public void loadMoreFinish(boolean emptyResult, boolean hasMore) {
        mLoadMoreContainer.loadMoreFinish(emptyResult, hasMore);
    }

    @Override
    public void loadMoreError() {
        mLoadMoreContainer.loadMoreError(0, UIUtils.getString(R.string.load_more_failed));
    }

    @Override
    public void showDialog() {
        if (null != mDialog && !mDialog.isShowing()) {
            mDialog.show();
        }
    }

    @Override
    public void dismissDialog() {
        if (null != mDialog && mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        AgentsManager.getInstance()
                .setOnButtonClickListener(null);
    }
}
